/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.zrnecko.bowlingnumbers.entity;

import java.util.StringTokenizer;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;

/**
 *
 * @author zrnecko
 */
@Name("frame")
public class Frame {

	@Logger
	private static Log log;

	private boolean pinDetail;

	private static final String FRAME_STRING_SEPARATOR = ";";

	private boolean tenth;

	private int strikeShotPinFall;

	private int spareShotPinFall;

	private int thirdShotPinFall;

	private boolean strike;

	private boolean spare;

	public Frame(String frameString, boolean tenth, boolean pinDetail) {
		if (pinDetail) {
			parsePinFrame(frameString, tenth);
		} else {
			parseFrame(frameString, tenth);
		}

	}

	private void parseFrame(String frameString, boolean tenth) {

	}

	private void parsePinFrame(String frameString, boolean tenth) {
		String framePattern = null;
		if (!tenth) {
			framePattern = "(X|[0-9]{1,10};([0-9]{1,10}|/|-))";
		} else {
			framePattern = "(X;X;X|" + "X;X;[0-9]{1,10}|" + "X;X;-|"
					+ "X;[0-9]{1,10};([0-9]{1,10}|-|/)|" + "[0-9]{1,10};-)|"
					+ "[0-9]{1,10};[0-9]{1,10}|"
					+ "[0-9]{1,10};/;(X|[0-9]{1,10})|";
		}
		if (!frameString.matches(framePattern)) {
			throw new InvalidFrameTokenException(
					"Frame string not valid! Frame must match: " + framePattern);
		}
		StringTokenizer frameTokenizer = new StringTokenizer(frameString,
				FRAME_STRING_SEPARATOR);
		String strikeShotString = null;
		String spareShotString = null;
		String thirdShotString = null;
		if (!tenth) {
			strikeShotString = frameTokenizer.nextToken();
			if (strikeShotString.equals("X")) {
				strikeShotPinFall = 10;
				spareShotPinFall = 0;
				setStrike(true);
			} else {
				setStrike(false);
				strikeShotPinFall = 10 - strikeShotString.length();
				spareShotString = frameTokenizer.nextToken();
				if (spareShotString.equals("/")) {
					spareShotPinFall = 10 - strikeShotPinFall;
					setSpare(true);
				} else {
					setSpare(false);
					if (spareShotString.equals("-")) {
						spareShotPinFall = 0;
					} else {
						spareShotPinFall = 10 - strikeShotPinFall
								- spareShotString.length();
					}

				}

			}
			log.debug("StrikeShotPinFall: " + strikeShotPinFall);
			log.debug("SpareShotPinFall: " + spareShotPinFall);

		} else {
			strikeShotString = frameTokenizer.nextToken();
			if (strikeShotString.equals("X")) {
				strikeShotPinFall = 10;
				spareShotString = frameTokenizer.nextToken();
				if (spareShotString.equals("X")) {
					spareShotPinFall = 10;
					thirdShotString = frameTokenizer.nextToken();
					// two strikes
					if (thirdShotString.equals("X")) {
						thirdShotPinFall = 10;
					} else if (thirdShotString.equals("-")) {
						thirdShotPinFall = 0;
					} else {
						thirdShotPinFall = 10 - thirdShotString.length();
					}
				} else {
					spareShotPinFall = 10 - spareShotString.length();
					thirdShotString = frameTokenizer.nextToken();
					if (thirdShotString.equals("/")) {
						thirdShotPinFall = 10 - spareShotPinFall;
					} else {
						if (thirdShotString.equals("-")) {
							thirdShotPinFall = 0;
						} else {
							thirdShotPinFall = 10 - spareShotPinFall
									- thirdShotString.length();
						}
					}
				}
			} else {
				strikeShotPinFall = 10 - strikeShotString.length();
				spareShotString = frameTokenizer.nextToken();
				if (spareShotString.equals("/")) {
					spareShotPinFall = 10 - strikeShotPinFall;
					thirdShotString = frameTokenizer.nextToken();
					if (thirdShotString.equals("X")) {
						thirdShotPinFall = 10;
					} else if (thirdShotString.equals("-")) {
						thirdShotPinFall = 0;
					} else {
						thirdShotPinFall = 10 - thirdShotString.length();
					}

				} else {
					if (spareShotString.equals("-")) {
						spareShotPinFall = 0;
					} else {
						spareShotPinFall = 10 - strikeShotPinFall
								- spareShotString.length();
					}
				}

			}
			log.debug("StrikeShotPinFall: " + strikeShotPinFall);
			log.debug("SpareShotPinFall: " + spareShotPinFall);
			log.debug("ThirdShotPinFall: " + thirdShotPinFall);

		}

	}

	public boolean isTenth() {
		return tenth;
	}

	public void setTenth(boolean tenth) {
		this.tenth = tenth;
	}

	/**
	 * @return the spare
	 */
	public boolean isSpare() {
		return spare;
	}

	/**
	 * @param spare
	 *            the spare to set
	 */
	public void setSpare(boolean spare) {
		this.spare = spare;
	}

	/**
	 * @return the spareShotPinFall
	 */
	public int getSpareShotPinFall() {
		return spareShotPinFall;
	}

	/**
	 * @param spareShotPinFall
	 *            the spareShotPinFall to set
	 */
	public void setSpareShotPinFall(int spareShotPinFall) {
		this.spareShotPinFall = spareShotPinFall;
	}

	/**
	 * @return the strike
	 */
	public boolean isStrike() {
		return strike;
	}

	/**
	 * @param strike
	 *            the strike to set
	 */
	public void setStrike(boolean strike) {
		this.strike = strike;
	}

	/**
	 * @return the strikeShotPinFall
	 */
	public int getStrikeShotPinFall() {
		return strikeShotPinFall;
	}

	/**
	 * @param strikeShotPinFall
	 *            the strikeShotPinFall to set
	 */
	public void setStrikeShotPinFall(int strikeShotPinFall) {
		this.strikeShotPinFall = strikeShotPinFall;
	}

	/**
	 * @return the thirdShotPinFall
	 */
	public int getThirdShotPinFall() {
		return thirdShotPinFall;
	}

	/**
	 * @param thirdShotPinFall
	 *            the thirdShotPinFall to set
	 */
	public void setThirdShotPinFall(int thirdShotPinFall) {
		this.thirdShotPinFall = thirdShotPinFall;
	}

	

}
